//
//  TabbarViewController.m
//  Weibo2
//
//  Created by Gate on 15/11/3.
//  Copyright © 2015年 Gate. All rights reserved.
//

#import "TabbarViewController.h"
#import "HomeViewController.h"
#import "MessageViewController.h"
#import "DiscoverViewController.h"
#import "MineViewController.h"
#import "TabBar.h"
#import "MyNavigationController.h"
#import "ComposeViewController.h"
@interface TabbarViewController ()<TabBarDelegate>
@property (nonatomic,strong) TabBar *customTabBar;
@end

@implementation TabbarViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setupTabBar];
    [self setupAllChildViewControllers];
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    for (UIView *child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            [child removeFromSuperview];
        }
    }
}
#pragma mark - 监听tabBar按钮的改变
- (void)tabBar:(TabBar *)tabBar didselectedButtonFrom:(int)from To:(int)to{
    NSLog(@"-------------%d-----%d",from,to);
    self.selectedIndex = to;
}

- (void)tabBarDidClickedPlusButton:(TabBar *)tabBar
{
    ComposeViewController *compose = [[ComposeViewController alloc] init];
    MyNavigationController *nav = [[MyNavigationController alloc] initWithRootViewController:compose];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)setupTabBar{
    
    TabBar *myTabBar = [[TabBar alloc]init];
//    myTabBar.backgroundColor = [UIColor redColor];
    myTabBar.delegate = self;

    myTabBar.frame = self.tabBar.bounds;
    [self.tabBar addSubview:myTabBar];
    self.customTabBar = myTabBar;
    
}
- (void)setupAllChildViewControllers{
    //初始化子控制器
    //1.首页
    HomeViewController *home = [[HomeViewController alloc] init];
//    home.tabBarItem.badgeValue = @"19";
    [self setupChildViewController:home title:@"首页" imageName:@"tabbar_home_os7" selectedImageName:@"tabbar_home_selected_os7"];
    
    //2.消息
    MessageViewController *message = [[MessageViewController alloc] init];
//    message.tabBarItem.badgeValue = @"10";
    [self setupChildViewController:message title:@"消息" imageName:@"tabbar_message_center_os7" selectedImageName:@"tabbar_message_center_selected_os7"];
    //3.广场
    DiscoverViewController *square = [[DiscoverViewController alloc] init];
    [self setupChildViewController:square title:@"广场" imageName:@"tabbar_discover_os7"
                 selectedImageName:@"tabbar_discover_selected_os7"];
    //4.我
    MineViewController *mine = [[MineViewController alloc] init];
    [self setupChildViewController:mine title:@"我" imageName:@"tabbar_profile_os7" selectedImageName:@"tabbar_profile_selected_os7"];
}

- (void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName{
    
    childVc.title = title;
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    MyNavigationController *nav = [[MyNavigationController alloc]initWithRootViewController:childVc];
    [self addChildViewController:nav];
    //添加tabbar内部按钮
    [self.customTabBar addTabBarButtonWithItem:childVc.tabBarItem];
    
}


@end
